<?php
/**
* @file $Id: TeacherCompletion.php 295 2006-11-15 04:39:23Z focus-sis $
* @package Focus/SIS
* @copyright Copyright (C) 2006 Andrew Schmadeke. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.txt
* Focus/SIS is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.txt for copyright notices and details.
*/

$grad_subjects_RET = DBGet(DBQuery("SELECT ID,TITLE,CREDITS,'' AS REMAINING_CREDITS FROM GRAD_SUBJECTS WHERE SCHOOL_ID='".UserSchool()."'"));

/*
$subject_select =  "<SELECT name=subject_id><OPTION value=''>"._('All Subjects')."</OPTION>";
foreach($grad_subjects_RET as $subject)
	$subject_select .= "<OPTION value=$subject[ID]".(($_REQUEST['subject_id']==$subject['ID'])?' SELECTED':'').">".$subject['TITLE']."</OPTION>";
$subject_select .= "</SELECT>";
*/

DrawHeader(ProgramTitle());

$tmp_PHP_SELF = PreparePHP_SELF('',array('mp'));
$mp_terms = array('SCHOOL_YEARS'=>_('Full-Year Grades'),'SCHOOL_SEMESTERS'=>_('Semester Grades'),'SCHOOL_QUARTERS'=>_('Quarter Grades'));

$sem = GetParentMP('SEM',UserMP());
if(!GetMP($sem,'START_DATE'))
	unset($mp_terms['SCHOOL_SEMESTERS']);
elseif(!$_REQUEST['mp'])
	$_REQUEST['mp'] = 'SCHOOL_SEMESTERS';

$fy = GetParentMP('FY',$sem);
if(!GetMP($fy,'START_DATE'))
	unset($mp_terms['SCHOOL_YEARS']);
elseif(!$_REQUEST['mp'])
	$_REQUEST['mp'] = 'SCHOOL_YEARS';

if(!GetMP(UserMP(),'START_DATE'))
	unset($mp_terms['SCHOOL_QUARTERS']);
elseif(!$_REQUEST['mp'])
	$_REQUEST['mp'] = 'SCHOOL_QUARTERS';

foreach($mp_terms as $table=>$mp_title)
{
	if($_REQUEST['mp']!==$table)
	{
		$tabcolor = 'DFDFDF';
		$textcolor = '666666';
	}
	else
	{
		$tabcolor = Preferences('HIGHLIGHT');
		$textcolor = 'FFFFFF';
	}

	$header .= '<TD width=10></TD><TD>'.DrawTab($mp_title,$tmp_PHP_SELF.'&amp;mp='.$table,$tabcolor,$textcolor,'_circle',array('tabcolor'=>Preferences('HIGHLIGHT'),'textcolor'=>'FFFFFF')).'</TD>';
}

echo TabsHeader($header);

$mps = GetAllTerms($_REQUEST['mp']);

//echo "<FORM action=Modules.php?modname=$_REQUEST[modname] method=POST>";
//DrawHeader($subject_select.' <INPUT type=submit value='._('Go').'>');
//echo '</FORM>';

Widgets('all');
$extra['link']['FULL_NAME'] = false;

if(!$_REQUEST['search_modfunc'] || $_REQUEST['search_modfunc']=='search')
{
	Search('student_id',$extra);
}
elseif($_REQUEST['search_modfunc']=='list')
{
	$sql = "SELECT ssm.STUDENT_ID,gs.TITLE,gs.ID,gs.CREDITS-COALESCE(sum(srcg.CREDITS),0) AS CREDITS
			FROM STUDENT_ENROLLMENT ssm LEFT OUTER JOIN GRAD_SUBJECTS gs ON (gs.SCHOOL_ID=ssm.SCHOOL_ID) LEFT OUTER JOIN STUDENT_REPORT_CARD_GRADES srcg ON (gs.SCHOOL_ID=srcg.SCHOOL_ID AND gs.ID=srcg.GRAD_SUBJECT_ID AND ssm.STUDENT_ID=srcg.STUDENT_ID AND srcg.CARRIES_CREDITS='Y' AND srcg.MARKING_PERIOD_ID IN (".$mps.")) WHERE ssm.SCHOOL_ID='".UserSchool()."' GROUP BY ssm.STUDENT_ID,gs.ID,gs.TITLE,gs.CREDITS HAVING (gs.CREDITS-COALESCE(sum(srcg.CREDITS),0))>0";
	
	if(User('PROFILE')=='parent' || User('PROFILE')=='student')
	{
		$RET = DBGet(DBQuery($sql),array(),array('ID'));
		foreach($grad_subjects_RET as $i=>$subject)
		{
			if(!$subject['CREDITS'])
			{}
			elseif(!$RET[$subject['ID']][1]['CREDITS'])
				$grad_subjects_RET[$i]['REMAINING_CREDITS'] = '<IMG SRC=assets/check.gif>';
			else
				$grad_subjects_RET[$i]['REMAINING_CREDITS'] = $RET[$subject['ID']][1]['CREDITS'];
		}
		
		$columns = array('TITLE'=>_('Requirement'),'CREDITS'=>_('Credits Required to Graduate'),'REMAINING_CREDITS'=>_('Credits Remaining to be Taken'));
		ListOutput($grad_subjects_RET,$columns,_('graduation requirement'),_('graduation requirements'));			
	}
	else
	{
		$RET = DBGet(DBQuery($sql),array(),array('STUDENT_ID','ID'));
		
		$students_RET = GetStuList();

		if(count($students_RET))
		{
			foreach($students_RET as $i=>$student)
			{
				if($RET[$student['STUDENT_ID']])
				{
					foreach($grad_subjects_RET as $grad_subject)
					{
						if(!$grad_subject['CREDITS'])
						{}
						elseif(!$RET[$student['STUDENT_ID']][$grad_subject['ID']][1]['CREDITS'])
							$students_RET[$i][$grad_subject['ID']] = '<IMG SRC=assets/check.gif>';
						else
							$students_RET[$i][$grad_subject['ID']] = $RET[$student['STUDENT_ID']][$grad_subject['ID']][1]['CREDITS'];
					}
				}
				else
					unset($students_RET[$i]);
			}
			$students_RET = ReindexResults($students_RET);
		}
		
		$columns = array('FULL_NAME'=>_('Student'),'STUDENT_ID'=>_('Student ID'),'GRADE_ID'=>_('Grade'));
		if(!$_REQUEST['period'])
		{
			foreach($grad_subjects_RET as $grad_subject)
				$columns[$grad_subject['ID']] = $grad_subject['TITLE'].' ('.$grad_subject['CREDITS'].')';
		}
		
		ListOutput($students_RET,$columns,_('student with unfulfilled graduation requirements'),_('students with unfulfilled graduation requirements'));
	}
}



?>